package GearCommands;

import Connections.Peer;
import Gear.Factory;
import Gear.Gear;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Implementation of the user command startaspeer.
 * Sets a gear program to run as a peer.
 *
 * @author Rodrigo Martins Racanicci
 */
public class GearCommandStartAsPeer implements GearCommand {

    @Override
    public void execute(String[] args, Gear gear) {
        try {
            if (args.length > 1) throw new IllegalArgumentException("Too much arguments to startaspeer command.");

            new Thread(Factory.getInstance().getPeerServer()).start();
            Thread.sleep((long) 1000.0);
            //Peer.getInstance().syncronizeFiles();
            Peer.getInstance().iAmAlive().sycronizeSharedFolder().send();

        } catch (IllegalArgumentException ex) {
            System.out.println(ex.getMessage());
        } catch (InterruptedException ex) {
            Logger.getLogger(Gear.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
